// pages/orders/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: "全部订单",
        isactive: true,
      },
      {
        id: 1,
        value: "待支付",
        isactive: false,
      },
      {
        id: 2,
        value: "待收货",
        isactive: false,
      },
      {
        id: 3,
        value: "退款/退货",
        isactive: false,
      },
    ],
    ordersList: [],
    // 页面编号
    type: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let ordersList = wx.getStorageSync("orders");
    this.setData({
      ordersList,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取页面栈
    let pages =  getCurrentPages();
    // console.log(pages)
    let thisPage = pages[pages.length-1]
    let type = thisPage.options.type
    // console.log(type);
    this.changeNav(type)
    this.getOrder(type)  
  },


  //改变tab状态
  handleChangActive(e) {
    wx.showLoading({
      title: "数据加载中",
    });
    // 获取子组件传递的索引值
    const { index } = e.detail;
  
    this.changeNav(index);
    this.getOrder(index)

    wx.hideLoading();
  },
  // 改变导航状态
  changeNav(index){
    const { tabs } = this.data;

      // 修改原数组
      tabs.forEach((v, i) =>
      i == index ? (v.isactive = true) : (v.isactive = false)
    );
    // 重新赋值
    this.setData({
      tabs,
      type: index,
    });
  },
  // 获取改变导航栏后的订单
  getOrder(type) {
    // 获取所有的订单
    let orders = wx.getStorageSync("orders") || [];
    if (type === 1) {
      // 显示代付款订单
      const order = orders.filter((v) => v.type == 1);
      // console.log(order);
      this.setData({
        ordersList: order,
      });
    } else if (type === 2) {
      // 显示以付款待收货
      const order = orders.filter((v) => v.type == 2);
      console.log(order);
      this.setData({
        ordersList: order,
      });
    } else {
      // 显示全部订单
      this.setData({
        ordersList: orders,
      });
    }
  },
  // 改变支付状态
  handelChangePay(e) {
    // 获取付款订单编号
    const { number } = e.currentTarget.dataset;
    // 获取全部订单
    let orders = wx.getStorageSync('orders')||[];
    // 根据订单编号过滤订单，并改变订单成交时间
    orders = orders.filter((v) =>
      v.order_number == number
        ? (v.order_time = this.formatDate(new Date().getTime()))
        : v
    );
    // 改变订单状态
    orders = orders.filter((v) =>
      v.order_number == number ? (v.type = 2) : v
    );
    // 保存订单信息
    this.setData({
      ordersList:orders
    })
    //存入本地数据
    wx.setStorageSync('orders', orders)
  },
  // 时间格式化
  formatDate() {
    let date = new Date();
    let year = date.getUTCFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    let houres = date.getHours();
    let minutes = date.getMinutes();
    let seconds = date.getSeconds();
    return (
      year +
      "/" +
      month +
      "/" +
      day +
      " " +
      houres +
      ":" +
      minutes +
      ":" +
      seconds
    );
  },
});
